package com.pioneer.app.dev.osuser.user;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;

import config.BaseHibernateDAO;

/**
 * A data access object (DAO) providing persistence and search support for TUser
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see com.pioneer.app.dev.osuser.user.TUser
 * @author MyEclipse Persistence Tools
 */

public class TUserDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(TUserDAO.class);

	// property constants
	public static final String CODE = "code";

	public static final String LOGIN_NAME = "loginName";

	public static final String NICK_NAME = "nickName";

	public static final String NAME = "name";

	public static final String TELPHONE = "telphone";

	public static final String MOBIL = "mobil";

	public static final String EMAIL = "email";

	public static final String ADDRESS = "address";

	public static final String PASSWORE = "passwore";

	public static final String STATUS = "status";

	public static final String LAST_VISIT_TIME = "lastVisitTime";

	public static final String PHOTO_PATH = "photoPath";

	public static final String POST_NAME = "postName";

	public static final String IS_CRYPTOGRAPH = "isCryptograph";

	public void save(TUser transientInstance) {
		log.debug("saving TUser instance");
		Transaction tx=null;
		try {
		  tx=	getSession().beginTransaction();
			getSession().save(transientInstance);
			tx.commit();
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			tx.rollback();
			throw re;
		}
	}

	public void delete(TUser persistentInstance) {
		log.debug("deleting TUser instance");
		try {
			Transaction tx=getSession().beginTransaction();
			getSession().clear();
			getSession().delete(persistentInstance);
			tx.commit();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public TUser findById(java.lang.Integer id) {
		log.debug("getting TUser instance with id: " + id);
		try {
			TUser instance = (TUser) getSession().get(
					"com.pioneer.app.dev.osuser.user.TUser", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(TUser instance) {
		log.debug("finding TUser instance by example");
		try {
			List results = getSession().createCriteria(
					"com.pioneer.app.dev.osuser.user.TUser")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding TUser instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from TUser as model where model." + propertyName
					+ "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByCode(Object code) {
		return findByProperty(CODE, code);
	}

	public List findByLoginName(Object loginName) {
		return findByProperty(LOGIN_NAME, loginName);
	}

	public List findByNickName(Object nickName) {
		return findByProperty(NICK_NAME, nickName);
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findByTelphone(Object telphone) {
		return findByProperty(TELPHONE, telphone);
	}

	public List findByMobil(Object mobil) {
		return findByProperty(MOBIL, mobil);
	}

	public List findByEmail(Object email) {
		return findByProperty(EMAIL, email);
	}

	public List findByAddress(Object address) {
		return findByProperty(ADDRESS, address);
	}

	public List findByPasswore(Object passwore) {
		return findByProperty(PASSWORE, passwore);
	}

	public List findByStatus(Object status) {
		return findByProperty(STATUS, status);
	}

	public List findByLastVisitTime(Object lastVisitTime) {
		return findByProperty(LAST_VISIT_TIME, lastVisitTime);
	}

	public List findByPhotoPath(Object photoPath) {
		return findByProperty(PHOTO_PATH, photoPath);
	}

	public List findByPostName(Object postName) {
		return findByProperty(POST_NAME, postName);
	}

	public List findByIsCryptograph(Object isCryptograph) {
		return findByProperty(IS_CRYPTOGRAPH, isCryptograph);
	}

	public List findAll(String condition) {
		log.debug("finding all TUser instances");
		try {
			//String queryString = "from TUser where name like '%b%' order by name asc";
			String queryString =null;
			if(null!=condition){
				queryString = "from TUser where "+condition+"  order by name asc";
			}else{
				queryString = "from TUser  order by name asc";
			}
			
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setMaxResults(3);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public TUser merge(TUser detachedInstance) {
		log.debug("merging TUser instance");
		try {
			TUser result = (TUser) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(TUser instance) {
		log.debug("attaching dirty TUser instance");
		try {
			Transaction tx=null;
			tx=getSession().beginTransaction();
			getSession().clear();
			getSession().saveOrUpdate(instance);
			tx.commit();
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(TUser instance) {
		log.debug("attaching clean TUser instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	public static void main(String[]args){
		
		TUserDAO dao=new TUserDAO();
		/**
		TUser user=new TUser();
		user.setId(new Integer(2));
		user.setCode("222");
		user.setName("test2");
		dao.save(user);
		*/
		
		List users=dao.findAll(null);
		java.util.Iterator it=users.iterator();
		TUser vo=null;
		while(it.hasNext()){
			vo=(TUser)it.next();
			System.out.println("name="+vo.getName());
		}
		
	}
}